﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RentAWorker.Desktop.Entities;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using RentAWorker.Business.Interfaces;
using RentManagement;
using Microsoft.Practices.Composite.Regions;
using RentAWorker.Management.Infrastructure;

namespace RentReturn
{
    public class RentReturnPresentationModel : IRentReturnPresentationModel, INotifyPropertyChanged,
                                                    IClosingAware
    {
        private IUnityContainer Container { get; set; }

        public DelegateCommand<object> ConfirmCommand { get; set; }

        public IRentRequestService RequestService { get; set; }

        public RentReturnPresentationModel(IUnityContainer container, IRentReturnView view,
                                            IRentRequestService requestSvc)
        {
            this.Container = container;
            this.View = view;
            this.View.Model = this;

            this.ConfirmCommand = new DelegateCommand<object>(Confirm,
                                        Confirm_CanExecute);
            RentCommands.ConfirmAllCommand.RegisterCommand(this.ConfirmCommand);

            this.RequestService = requestSvc;
        }

        public void Initialize(RentalService rent)
        {
            this.RentalService = rent;
        }

        private void Confirm(object nullparam)
        {
            RentCommands.ConfirmAllCommand.UnregisterCommand(this.ConfirmCommand);

            IRegionManager regionManager = this.Container.Resolve<IRegionManager>();
            regionManager.Regions["RentalList"].Remove(this.View);
        }

        private bool Confirm_CanExecute(object nullparam)
        {
            return this.ReturnedOK || this.ReturnedRuined;
        }

        #region Bound Properties
        private bool _returnedOK = default(bool);
        public bool ReturnedOK
        {
            get
            {
                return _returnedOK;
            }
            set
            {
                _returnedOK = value;
                OnPropertyChanged("ReturnedOK");
                this.ConfirmCommand.RaiseCanExecuteChanged();
            }
        }

        private bool _returnedRuined = default(bool);
        public bool ReturnedRuined
        {
            get
            {
                return _returnedRuined;
            }
            set
            {
                _returnedRuined = value;
                OnPropertyChanged("ReturnedRuined");
                this.ConfirmCommand.RaiseCanExecuteChanged();
            }
        }

        private RentalService _rentalService = default(RentalService);
        public RentalService RentalService
        {
            get
            {
                return _rentalService;
            }
            set
            {
                _rentalService = value;
                OnPropertyChanged("RentalService");
            }
        } 
        #endregion


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        #region IRentReturnPresentationModel Members

        public IRentReturnView View { get; set; }

        #endregion

        #region IClosingAware Members

        public bool RequestClose()
        {
            return this.CanClose;
        }

        public bool CanClose
        {
            get { return true; }
        }

        #endregion
    }
}
